package solution1.demo1206;

import java.util.Arrays;

public class Solution {

    //插入排序
    public static void insertSort(int[] arr) {
        for(int i = 1; i < arr.length; i++) {
            int key = arr[i];
            int j = i - 1;
            for(; j >= 0; j--) {
                if(arr[j] > key) {
                    arr[j + 1] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + 1] = key;
        }
    }

    //希尔排序
    public static void shellSort(int[] arr) {
        int key = arr.length;
        while(key > 1) {
            key >>= 1;
            shellInsertSort(arr, key);
        }
        shellInsertSort(arr, 1);
    }

    private static void shellInsertSort(int[] arr, int key) {
        for(int i = key; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i - key;
            for(; j >= 0; j -= key) {
                if(arr[j] > tmp) {
                    arr[j + key] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + key] = tmp;
        }
    }

    //冒泡排序
    public static void bubbleSort(int[] arr) {
        int flag = 1; //假设升序
        for(int i = 0; i < arr.length; i++) {
            for(int j = 0; j < arr.length - i - 1; j++) {
                if(arr[j] > arr[j + 1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                    flag = 0;
                }
            }
            if(flag == 1) {
                break;
            }
        }
    }

    //选择排序
    public static void selectSort(int[] arr) {
        for(int i = 0; i < arr.length; i++) {
            int index = i;
            for(int j = i + 1; j < arr.length; j++) {
                if(arr[index] > arr[j]) {
                    index = j;
                }
            }
            swap(arr, i, index);
        }
    }

    private static void swap(int[] arr, int i, int index) {
        int tmp = arr[i];
        arr[i] = arr[index];
        arr[index] = tmp;
    }

    //堆排序
    public static void heapSort(int[] arr) {
        //创建一个大根堆
        createBigHeap(arr);
        int end = arr.length - 1;
        while(end > 0) {
            swap(arr, 0, end);
            adjustDown(arr, 0, end);
            end--;
        }
    }

    private static void createBigHeap(int[] arr) {
        int len = arr.length;
        for(int parent = (len - 1 - 1) / 2; parent >= 0; parent--) {
            adjustDown(arr, parent, len);
        }
    }

    private static void adjustDown(int[] arr, int parent, int len) {
        int child = parent * 2 + 1;
        while(child < len) {
            if(child + 1 < len && arr[child] < arr[child + 1]) {
                child++;
            }
            if(arr[child] > arr[parent]) {
                swap(arr, child, parent);
                parent = child;
                child = child * 2 + 1;
            } else {
                break;
            }
        }
    }

    public static void quickSort(int[] arr) {
        quick(arr, 0, arr.length - 1);
    }

    private static void quick(int[] arr, int left, int right) {
        if(left >= right) {
            return;
        }
        //三数取中优化
        int mid = fundMid(arr, left, right);
        swap(arr, left, mid);
        //1.寻找基准
        int point = fundPoint(arr, left, right);
        quick(arr, point + 1, right);
        quick(arr, left, point - 1);
    }

    private static int fundMid(int[] arr, int left, int right) {
        int mid = (left + right) / 2;
        if(arr[left] < arr[right]) {
            if(arr[mid] < arr[left]) {
                return left;
            } else if(arr[mid] > arr[right]) {
                return right;
            } else {
                return mid;
            }
        } else {
            if(arr[mid] > arr[left]) {
                return left;
            } else if(arr[mid] < arr[right]) {
                return right;
            } else {
                return mid;
            }
        }
    }

    private static int fundPoint(int[] arr, int left, int right) {
        int key = arr[left];
        while(left < right) {
            while(left < right && arr[right] >= key) {
                right--;
            }
            arr[left] = arr[right];
            while(left < right && arr[left] <= key) {
                left++;
            }
            arr[right] = arr[left];
        }
        arr[left] = key;
        return left;
    }

    public static void mergeSort(int[] arr) {
        mergeFun(arr, 0, arr.length - 1);
    }

    private static void mergeFun(int[] arr, int left, int right) {
        if(left >= right) {
            return;
        }
        int mid = (left + right) / 2;
        mergeFun(arr, left, mid);
        mergeFun(arr, mid + 1, right);
        merge(arr, left, right, mid);
    }

    private static void merge(int[] arr, int left, int right, int mid) {
        int[] tmpArr = new int[right - left + 1];
        int index = 0;
        int s1 = left;
        int s2 = mid + 1;
        while(s1 <= mid && s2 <= right) {
            if(arr[s1] < arr[s2]) {
                tmpArr[index++] = arr[s1++];
            } else {
                tmpArr[index++] = arr[s2++];
            }
        }
        while(s1 <= mid) {
            tmpArr[index++] = arr[s1++];
        }
        while(s2 <= right) {
            tmpArr[index++] = arr[s2++];
        }
        for(int i = 0; i < index; i++) {
            arr[left + i] = tmpArr[i];
        }
    }

    public static void main(String[] args) {
        int[] arr = {3,5,1,7,6,2,8,9,4,5};
        mergeSort(arr);
        System.out.println(Arrays.toString(arr));
    }

}
